<?php
include 'includes/cPage.php';
include 'includes/cDatabase.php';
include 'includes/variables.php';
include 'includes/cPlayer.php';
include 'includes/cMember.php';
include 'includes/cGame.php';
include 'includes/cTransfer.php';
include 'includes/cDates.php';

$cPage = new page("Transfer Activity");
echo $cPage->getTitle();
echo $cPage->pageStringStart();
echo $cPage->contentStringStart();

$cDatabase = new database();

$connect = $cDatabase->connectToDatabase();
$activeDatabase = $cDatabase->getActiveDatabase($connect);
	
$db = $cDatabase->selectDatabase($connect, $activeDatabase);

$selectArray[0] = '*';
$result = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, NULL, NULL, NULL, NULL);

$cPlayer = array();
$mostPopularEver = array();
$mostPopularIn = array();
$mostPopularOut = array();

for ($i = 0; $i < mysqli_num_rows($result); $i++)
{
	$row = mysqli_fetch_array($result, MYSQLI_ASSOC);

	$cPlayer[$i] = new player($row['playerCode']);
		
	$error |= $cPlayer[$i]->addPlayerName($row['playerName']);
	$error |= $cPlayer[$i]->addPlayerTeam($row['playerTeam']);
	$error |= $cPlayer[$i]->addPlayerValue($row['playerValue']);
	$error |= $cPlayer[$i]->addPlayerPosition($row['playerPosition']);
	$error |= $cPlayer[$i]->addPlayerLastYearScore($row['playerLastYearScore']);
	
	//$cPlayer[$i]->loadPlayerOccurrence($connect);

	//if (($cPlayer[$i]->In != 0) || ($cPlayer[$i]->Out != 0))
	//{
	//	echo "<BR>".$cPlayer[$i]->getPlayerName()." transferred in by ".$cPlayer[$i]->In." people";
	//	echo "<BR>".$cPlayer[$i]->getPlayerName()." transferred out by ".$cPlayer[$i]->Out." people";
	//}
}

//Loop
// Find from memberList all case where playerWeekOut = 1
// Get PlayerCodeOut and PlayerCodeIn	
$cDates = new dates;
$currentWeek = $cDates->getCurrentWeek();

for ($week = $currentWeek; $week > 0/*$cDatabase->GetMaxWeeks($connect)*/; $week--)
{	
	$queryTotal = " SELECT * FROM memberlist where ";
	for ($i = 1; $i <= MAXPLAYERS; $i++)
	{
		$subQueryTotal = getPlayerWeekOutVariable($i)." = '".$week."'";
		if ($i != MAXPLAYERS)
		{
			$subQueryTotal = $subQueryTotal." OR ";
		}
		$queryTotal = $queryTotal.$subQueryTotal;
	}
	//echo "<BR>".$queryTotal;
	$result = mysqli_query($connect, $queryTotal);
	$totalTransfers = mysqli_num_rows($result);
	
	echo "<BR><BR><strong>Week $week: Number Transfers $totalTransfers</strong>";
	
	for ($j = 1; $j <= mysqli_num_rows($result); $j++)
	{
		$playerOutCount = 0;
		$playerInCount = 0;
		$playerOut = array();
		$playerIn = array();
		
		$row = mysqli_fetch_array($result, MYSQLI_ASSOC);
		echo "<BR><BR>$j ".$row['memberName']." - ".$row['teamName'];
		
		// We've got the row, now find which players were transferred for this week
		for ($i = 1; $i <= MAXPLAYERS; $i++)
		{
			if ($row[getPlayerWeekOutVariable($i)] == $week)
			{
				// This player was transferred out for this $week
				//echo "OUT: ".$row[getPlayerCodeVariable($i)];
				$playerOut[$playerOutCount] = $row[getPlayerCodeVariable($i)];
				$playerOutCount++;
			}
			if ($row[getPlayerWeekInVariable($i)] == $week)
			{
				// This player was transferred in for this $week
				//echo "IN: ".$row[getPlayerCodeVariable($i)];
				$playerIn[$playerInCount] = $row[getPlayerCodeVariable($i)];
				$playerInCount++;
			}
		}
		$selectArray[0] = 'playerName';
		for ($i = 0; $i < $playerInCount; $i++)
		{
			$playerResult = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, "playerCode", $playerOut[$i], NULL, NULL);
			$row = mysqli_fetch_array($playerResult, MYSQLI_ASSOC);
			echo "<BR>OUT:".$playerOut[$i]." ".$row['playerName'];
			
			$playerResult = $cDatabase->selectFromDatabase($connect, 'playerlist', $selectArray, "playerCode", $playerIn[$i], NULL, NULL);
			$row = mysqli_fetch_array($playerResult, MYSQLI_ASSOC);
			echo " IN:". $playerIn[$i]." ".$row['playerName'];;
		}
	}
}
		
echo $cPage->getContentStringEnd();
echo $cPage->getMenu();
echo $cPage->getClear();
echo $cPage->pageStringEnd();
echo $cPage->getFooter();
?>